// pages/payment/payment.js
const app=getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price: 1,
    key:1,
    payList: [
      { id: 2, payPic: "../../icons/WeChat.png", payName: "微信" }
    ]
  },
  click:function(e){
    var index = e.currentTarget.dataset.index;
    var that=this;
    if(this.data.key==1){
      that.setData({
        key: 0
      })
    }else{
      that.setData({
        key:1
      })
    }
    console.log(this.data.key)
   
  },
  post: function () {
   var that=this;
    util.showLoading('加载中...');
   console.log(that.data.orderId);
   
    var price = (parseFloat(this.data.price * 100).toFixed(0)).toString();
  // var price=1;
  //  price=price.toString();
  // console.log(price);
    wx.request({
      // url: "https://www.xiaozheng8.com/serviceOrder/noPay/v2",
      url: "https://www.xiaozheng8.com/serviceOrder/setStatus/v3",
      // url: 'https://www.poi58.com/price/weixin/pay/good/wxpay.php?id=' + "o0Sl75WxgNrx0EMFx9odeu_urWuc" + '&fee=' + "1" + "&orderno=" + "00" + "&bookid=" + "1",
      // header: {
      //   'Content-Type': 'application/x-www-form-urlencoded'
      // },
      
      data:{
        orderId:that.data.orderId,
         totalFee:price,
        // totalFee:"1",
        status:10
      },
      method: 'POST',
      success: function (res) {
        util.hideLoading();
        console.log(JSON.stringify(res.data));
        console.log('调起支付');
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          "appId":res.data.appId,
          'paySign': res.data.paySign,
          // "timeStamp": "1560311274280",
          // "package": "prepay_id=wx12114754088097eeb61752ef1325540100",
          // "paySign": "6C9C18BDADCF189D1A9CF1F8A19A834F",
          // "appId": "wxd3ed036f899aa990",
          // "signType": "MD5",
          // "nonceStr": "uikCOT6cdkdrRsBf",
          'success': function (res) {
            console.log('success');
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000,
              success(){
                wx.redirectTo({
                  url: '../receipt/receipt?orderId=' + that.data.orderId + "&receiveUserId=" + that.data.receiveUserId,
                })
              }
            });
            
            //that.getVipInfo();
          },
          'fail': function (res) {
            wx.showToast({
              title: '支付失败',
              icon:"none"
            })
            console.log(res);
          },
          'complete': function (res) {
            console.log('complete');
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '支付失败',
          icon: "none"
        })
        console.log(res.data)
      }
    });



},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      price: options.price,
      orderId:options.orderId,
      status:options.status,
      receiveUserId: options.receiveUserId
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})